package com.youdianstar.app.common.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.util.Objects;

/**
 * File: ScreenUtil
 * Author: 82149 Create: 2021/9/22 18:22
 * Changes (from 2021/9/22)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class ScreenUtil {

    public static void getScreenGallery(Context c) {
        DisplayMetrics dm = c.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        Log.i("screen", "---width : " + screenWidth + "; height : " + screenHeight);
    }

    public static Point getScreenSize(Activity activity) {
        Point point = new Point();
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        defaultDisplay.getSize(point);
        return point;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context Activity context
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        Objects.requireNonNull(wm).getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕高度
     *
     * @param context Activity context
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        Objects.requireNonNull(wm).getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * Get real height of screen
     *
     * @param mContext Activity context
     */
    public static int getRealScreenHeight(Context mContext) {
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point outPoint = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            display.getRealSize(outPoint);
        } else {
            display.getSize(outPoint);
        }
        int mRealSizeHeight;
        mRealSizeHeight = outPoint.y;
        return mRealSizeHeight;
    }

    /**
     * 获取两个控件键间距
     *
     * @param v1 控件1 高位控件
     * @param v2 控件2 低位控件
     */
    public static int getSpaceHeight(Context mContext, View v1,View v2){
        int[] viewLocation = new int[2];
        v1.getLocationInWindow(viewLocation);
        int v1X = viewLocation[0]; // x 坐标
        int v1Y = viewLocation[1]; // y 坐标
        int[] viewLocation2 = new int[2];
        v2.getLocationInWindow(viewLocation2);
        int v2X = viewLocation2[0]; // x 坐标
        int v2Y = viewLocation2[1]; // y 坐标
        return v2Y-v1Y;
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public static float dp2px(Context mContext, float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpValue, mContext.getResources().getDisplayMetrics());
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param pxValue
     * @param pxValue （DisplayMetrics类中属性density）
     * @return
     */
    public static int px2dip(Context mContext,  float pxValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
